% function bestand = make_psatfile(filename)
% filename = [filename,'.m'];
%load netwerkdata;

fid=fopen(filename,'wt');

%print Bus.con
count = fprintf(fid, 'Bus.con = [ ...\n');
format =['%4d %8.4g %8.5g %8.5g %4u %4u;\n'];
count = fprintf(fid,format,Bus.con');
count = fprintf(fid,'   ];\n\n');

%print SW.con
count = fprintf(fid, 'SW.con = [ ...\n');
format = ['%4d ',repmat('%8.4g ',1,10),'%2u %2u \n'];
count = fprintf(fid,format,SW.con');
count = fprintf(fid,'   ];\n\n');


%print PV.con
count = fprintf(fid, 'PV.con = [ ...\n');
format = ['%4d ',repmat('%8.4g ',1,9),'%2u;\n'];
count = fprintf(fid,format,PV.con');
count = fprintf(fid,'   ];\n\n');

if exist('Breaker','var')==1
%print Breaker.con
count = fprintf(fid, 'Breaker.con = [ ...\n');
count = fprintf(fid,'%4d %4.4g %4.4g %4.4g %4.4g %3.4g %2.4g %4.5g %6.5g %6.5g\n',Breaker.con');
% format = ['%4d ',repmat('%8.4g ',1,5),'%2u;\n'];
% count = fprintf(fid,format,Breaker.con');
count = fprintf(fid,'   ];\n\n');
end

%print PQ.con
count = fprintf(fid, 'PQ.con = [ ...\n');
format = ['%4d ',repmat('%8.4g ',1,6),'%2u %2u;\n'];
count = fprintf(fid,format,PQ.con');
count = fprintf(fid,'   ];\n\n');

if exist('Shunt','var')==1
%print Shunt.con
count = fprintf(fid, 'Shunt.con = [ ...\n');
format = ['%4d ',repmat('%8.4g ',1,5),'%2u;\n'];
count = fprintf(fid,format,Shunt.con');
count = fprintf(fid,'   ];\n\n');
end

if exist('Fault','var')==1
        %print Fault.con
        count = fprintf(fid, 'Fault.con = [... \n');
        count = fprintf(fid,'%4d %4.4g %4.4g %3.4g %2.4g %4.5g %6.5g %6.5g\n',Fault.con);
%         count = fprintf(fid,'\n');
        count = fprintf(fid,'   ];\n\n');
end

%print Line.con
count = fprintf(fid, 'Line.con = [ ...\n');
format = ['%4d %4d ',repmat('%8.4g ',1,13),'%2u;\n'];
count = fprintf(fid,format,Line.con');
count = fprintf(fid,'   ];\n\n');

if exist('Syn','var')==1
        %print Syn.con
        count = fprintf(fid, 'Syn.con = [... \n');
        format = [repmat('%8.4g ',1,length(Syn.con)),';\n'];
        count = fprintf(fid,format,Syn.con');
%         count = fprintf(fid,'\n');
        count = fprintf(fid,'   ];\n\n');
end
if exist('Exc','var')==1        
        %print Exc.con
        count = fprintf(fid, 'Exc.con = [... \n');
        format = [repmat('%8.4g ',1,length(Exc.con)),';\n'];
        count = fprintf(fid,format,Exc.con');
%         count = fprintf(fid,'\n');
        count = fprintf(fid,'   ];\n\n');
end
if exist('Tg.con','var')==1        
        %print Tg.con
        count = fprintf(fid, 'Tg.con = [... \n');
        format = [repmat('%8.4g ',1,length(Tg.con)),';\n'];
        count = fprintf(fid,format,Tg.con');
%         count = fprintf(fid,'\n');
        count = fprintf(fid,'   ];\n\n');
end

if exist('Statcom','var')==1
        %print Syn.con
        count = fprintf(fid, 'Statcom.con = [... \n');
        format = [repmat('%8.4g ',1,size(Statcom.con,2)),';\n'];
        count = fprintf(fid,format,Statcom.con');
%         count = fprintf(fid,'\n');
        count = fprintf(fid,'   ];\n\n');
end

if exist('Svc.con','var')==1
        %print Syn.con
        count = fprintf(fid, 'Svc.con = [... \n');
        format = [repmat('%8.4g ',1,length(Svc.con)),';\n'];
        count = fprintf(fid,format,Svc.con');
%         count = fprintf(fid,'\n');
        count = fprintf(fid,'   ];\n\n');
end

        %print Varname.idx
        count = fprintf(fid, 'Varname.idx = [... \n');
        format = [repmat('%8.4g; ',1,7),'\n'];
        count = fprintf(fid,format,Varname.idx');
%         count = fprintf(fid,'\n');
        count = fprintf(fid,'   ];\n\n');
        
        fprintf(fid,'Bus.names = { ...\n');
        for i = 1:length(Bus.names)
          fprintf(fid,'    ''%s'';\n',char(Bus.names(i,:)));
        end
        fprintf(fid,'   };\n\n');
        
        %print Areas.names
        fprintf(fid,'Areas.names = { ...\n');
        for i = 1:length(Areas.names(:,1))
          fprintf(fid,'    ''%s''\n',char(Areas.names(i,:)));
        end
        fprintf(fid,'   };\n\n');
        
        if exist('Areas.con','var')==1
        count = fprintf(fid, 'Areas.con = [... \n');
        format = [repmat('%8.4g ',1,8),';\n'];
        count = fprintf(fid,format,Areas.con);
%         count = fprintf(fid,'\n');
        count = fprintf(fid,'   ];\n\n');
        end


fclose(fid);